﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using BaMusaSoft.RPS.DataModel;
using BaMusaSoft.RPS.DataModel.Repositories;
using BaMusaSoft.RPS.ViewModel.Infrastructure;
using GalaSoft.MvvmLight.Command;
using System.ComponentModel;

namespace BaMusaSoft.RPS.ViewModel
{

    public class AddContractViewModel : Workspace
    {
        #region "Fields"

        private readonly RepositoryBase<Contract> _contractsRepository;
        private readonly IUnitOfWork _unitOfWork;
        private ObservableCollection<Contract> _contracts;
        private RelayCommand _searchCommand;
        private Contract _selectedContract;

        #endregion

        #region "Constructor"

        public AddContractViewModel(IUnitOfWork unitOfWork)
        {
            if (unitOfWork == null) throw new ArgumentNullException("unitOfWork");
            _unitOfWork = unitOfWork;
            _contractsRepository = _unitOfWork.Contracts;
            Contracts = new ObservableCollection<Contract>(_contractsRepository.GetAll());
        }

        #endregion

        #region "Properties"

        public string PropertyNo { get; set; }

        public ObservableCollection<Contract> Contracts
        {
            get { return _contracts; }
            private set
            {
                _contracts = value;
                RaisePropertyChanged("Contracts");
            }
        }

        public Contract SelectedContract
        {
            get { return _selectedContract; }
            set
            {
                _selectedContract = value;
                RaisePropertyChanged("SelectedContract");
            }
        }

        #endregion

        #region "Commands"

        public ICommand SearchCommand
        {
            get { return _searchCommand ?? (_searchCommand = new RelayCommand(SearchContracts)); }
        }

        #endregion

        #region "Methods"

        private void SearchContracts()
        {
            if (string.IsNullOrEmpty(PropertyNo)) return;
            Contracts = new ObservableCollection<Contract>(_contractsRepository.GetAll().Where(x => x.Property.PropertyNo.Contains(PropertyNo)));
        }

        #endregion

        #region Overrides of Workspace

        public override string DisplayName
        {
            get { throw new NotImplementedException(); }
            protected set { throw new NotImplementedException(); }
        }

        public override bool HasChanges
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        
    }
}